package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Option;
import com.sl.au.entity.PartsType;
import com.sl.au.entity.WarnStatus;
import com.sl.au.service.OptionService;
import com.sl.au.service.PartsTypeService;
import com.sl.au.service.WarnStatusService;

@RestController
@RequestMapping(value = "/ui/warnStatus")
public class WarnStatusController extends BaseController {

	
	@Autowired
	WarnStatusService warnStatusService;
	
	@Autowired
	PartsTypeService partsTypeService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("warnStatus/warnStatusIndex");
		List<PartsType> partsTypes = partsTypeService.getRepository().findAll();
		modelAndView.addObject("partsTypes", partsTypes);
		return modelAndView;
	}

	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		int count = 0;
		List<WarnStatus> warnStatus = null;
		warnStatus = warnStatusService.getPage(start, length, key, "createTime", Sort.Direction.DESC);
		count = warnStatusService.getCount(key);
		map.put("data", warnStatus);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	///保存
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public Map<String, Object> saveWarnStatus(HttpServletRequest request, @RequestBody WarnStatus warnStatus) {
		Map<String, Object> map = new HashMap<String, Object>();
		WarnStatus save = null;
		if(warnStatus.getId().equals("")) {
			save = new WarnStatus();
		}else {
			save = warnStatusService.getRepository().getOne(warnStatus.getId());
		}
		save.setWarnStatus(warnStatus.getWarnStatus());
		save.setWarnStatusName(warnStatus.getWarnStatusName());
		save.setPartsType(warnStatus.getPartsType());
		warnStatusService.getRepository().save(save);
		map.put("code", 0);
		map.put("msg", "警告类型保存成功！");
		return map;
	}

	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	public Map<String, Object> saveWarnStatus(HttpServletRequest request,@PathVariable String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		warnStatusService.getRepository().delete(id);
		map.put("code", 0);
		map.put("msg", "警告类型删除成功！");
		return map;
	}
}
